package com.bdqn.servlet;

import com.bdqn.dao.PetDao;
import com.bdqn.dao.impl.PetDaoImpl;
import com.bdqn.entity.Pet;
import com.bdqn.service.impl.PetServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

public class PetServlet extends HttpServlet {

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String action=req.getParameter("action");
        if ("allPet".equals(action)){
            this.getPetList(req,resp);
        }else if(("addPet").equals(action)){
            this.addPet(req,resp);
        }else if("deletePet".equals(action)){
            this.deletePet(req, resp);
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }

    //查询所有宠物信息方法
    public void getPetList(HttpServletRequest req, HttpServletResponse resp){
        PetServiceImpl petService=new PetServiceImpl();
        PetDao petDao=new PetDaoImpl();
        petService.setPetDao(petDao);
        List<Pet> petList=petService.getPetList();
        try {
            req.getSession().setAttribute("petlist",petList);
            req.getRequestDispatcher("petlist.jsp").forward(req,resp);
        } catch (ServletException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //添加宠物信息方法
    public void addPet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Date date=null;
        String petName=req.getParameter("petName");
        Integer petBreed=Integer.parseInt(req.getParameter("petBreed"));
        Integer petSex=Integer.parseInt(req.getParameter("petSex"));
        String time=req.getParameter("birthday");
        SimpleDateFormat format=new SimpleDateFormat("yyyy-MM-dd");
        try {
            date=format.parse(time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        String description=req.getParameter("description");
        Pet pet=new Pet();
        pet.setPetName(petName);
        pet.setPetBreed(petBreed);
        pet.setPetSex(petSex);
        pet.setBirthday(date);
        pet.setDescription(description);
        PetServiceImpl petService=new PetServiceImpl();
        PetDao petDao=new PetDaoImpl();
        petService.setPetDao(petDao);
        int count=petService.addPet(pet);
        if (count>0){
            try {
                resp.sendRedirect("PetServlet?action=allPet");
            } catch (IOException e) {
                e.printStackTrace();
            }
        }else {
            req.getRequestDispatcher("addPet.jsp").forward(req, resp);
        }
    }

    //删除宠物信息方法
     public  void deletePet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
         PetServiceImpl petService=new PetServiceImpl();
         PetDao petDao=new PetDaoImpl();
         petService.setPetDao(petDao);
         int count = petService.deletePet(Integer.parseInt(req.getParameter("petId")));
         if(count>0){
             resp.sendRedirect("PetServlet?action=allPet");
         }else{
             resp.sendRedirect("PetServlet?action=allPet");
         }
     }

}
